<?php
require('config.php');

		$sql="select * from `".$oto."_goods_cats` where catFlag=1 and isFixed=0 and isShow=1 order by parentId ASC,catSort ASC";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$list[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$yiji_num=0;
		for($i=0;$i<count($list);$i++)
		{
			if($list[$i]['parentId']==0)
			{
				$cat_id=$list[$i]['catId'];
				$time=date('Y-m-d');
				$sql="select * from `".$oto."_ads` where `adPositionId`='".$cat_id."' and `adStartDate`<='$time' and `adEndDate`>='$time'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$ads[$yiji_num][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($ads[$yiji_num]))
				{
					$list[$i]['cateImg']=$ads[$yiji_num][0]['adFile'];
					$list[$i]['adURL']=$ads[$yiji_num][0]['adURL'];
					$list[$i]['adId']=$ads[$yiji_num][0]['adId'];
					$list[$i]['adURLApp']=$ads[$yiji_num][0]['adURLApp'];
					$adURLAPP=0;
					$adURLAPP=explode('|',$ads[$yiji_num][0]['adURLApp']);
					$list[$i]['adURLApp_type']=$adURLAPP[0];
					$list[$i]['adURLApp_id']=$adURLAPP[1];
				}
				else
				{
					$list[$i]['cateImg']='';
					$list[$i]['adURL']='';
					$list[$i]['adId']='';
					$list[$i]['adURLApp']='';
					$list[$i]['adURLApp_type']='';
					$list[$i]['adURLApp_id']='';
				}
				$data[$yiji_num]=$list[$i];
				$erji_num=0;
				for($j=0;$j<count($list);$j++)
				{
					if($list[$i]['catId']==$list[$j]['parentId'])
					{
						$data[$yiji_num]['list'][$erji_num]=$list[$j];
						$sanji_num=0;
						for($k=0;$k<count($list);$k++)
						{
							if($list[$j]['catId']==$list[$k]['parentId'])
							{
								$data[$yiji_num]['list'][$erji_num]['list'][$sanji_num]=$list[$k];
								$sanji_num++;
							}
						}
						if($sanji_num==0) $data[$yiji_num]['list'][$erji_num]['list']=[];
						$erji_num++;
					}
				}
				if($erji_num==0) $data[$yiji_num]['list']=[];
				$yiji_num++;
			}
		}
		//print_r($data);
		echo json_encode($data);
?>